﻿using Nova_Common;
using Nova_DAL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Material_MaterialTypeAddOrEdit : System.Web.UI.Page
{
    Nova_BLL.MaterialType Nova_BLL_MaterialType = new Nova_BLL.MaterialType();
    string strMessage = "";

    DataSet ds = new DataSet();
    DbHelper dbHelper = new DbHelper();
    DbCommand cmd = null;
    string strSQL = "";
    int iCount = 0;  //操作结果

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = 0;  //0表示立即过期
        Response.Cache.SetNoStore();//禁用缓存
        Response.AppendHeader("Pragma", "no-cache");// 是不要网页存于缓存之中

        LoginLogic.IsLogin();

        if (!IsPostBack)
        {
            Bind_ddlParentType();
            SetFirstPageLoad();
        }

        //验证TextBox
        string strJs = "";
        strJs = "return Validate(";
        strJs += "'txtTypeID','类别编码不为空','r'";
        strJs += ",'txtTypeName','类别名称不为空','r'";
        strJs += ");";
        ibtnOK.Attributes.Add("onclick", strJs);
        ibtnContinue.Attributes.Add("onclick", strJs);
    }

    protected void ibtnOK_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            int res = 0;
            strMessage = "";

            if (Request.QueryString["Mode"] == "Add")     //写入操作
            {

                res = Nova_BLL_MaterialType.InsertMaterialType(txtTypeID.Text.Trim(), txtTypeName.Text.Trim(), ddlParentType.SelectedValue, out strMessage);
            }
            else if (Request.QueryString["Mode"] == "Update")  //更新操作
            {
                res = Nova_BLL_MaterialType.UpdateMaterialType(txtTypeID.Text.Trim(), txtTypeName.Text.Trim(), ddlParentType.SelectedValue, out strMessage);

            }

            if (res == 1) //增加成功或者修改成功
            {
                //ibtnOK.Attributes.Add("onclick", "javascript:window.returnValue=123;window.close()");
                this.Page.ClientScript.RegisterStartupScript(typeof(string), "Nova", "window.returnValue=123;top.close();", true);//关闭页面
                return;
            }
            else     //增加失败或者修改失败
            {
                ShowBox.MessageBox(this.Page, strMessage);
            }
        }
        catch (Exception ex)
        {
            ShowBox.MessageBox(this.Page, "继续增加异常跳出");
        }
    }

    /// <summary>
    ///  绑定父类别类型
    /// </summary>
    private void Bind_ddlParentType()
    {
        try
        {
            string strSQL = "select * from TMaterialType where ParentTypeID='0'";
            cmd = dbHelper.GetSqlStringCommond(strSQL);
            ds = dbHelper.ExecuteDataSet(cmd);
            ddlParentType.DataSource = ds;
            ddlParentType.DataBind();
            ddlParentType.Items.Insert(0, new ListItem("无所属类/创建父类别", "0"));
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }


    protected void ddlParentType_SelectedIndexChanged(object sender, EventArgs e)
    {
        strMessage = "";
        if (ddlParentType.SelectedValue == "0")   //获取新的父类别的ID编码
        {

            this.txtTypeID.Text = Nova_BLL_MaterialType.GetNewParentTypeID(out strMessage);
        }
        else     //获取该父类别下 新的子类别的ID编码
        {
            this.txtTypeID.Text = Nova_BLL_MaterialType.GetNewSubTypeID(ddlParentType.SelectedValue, out strMessage);
        }

        //没有获取到ID编码
        if (string.IsNullOrEmpty(this.txtTypeID.Text.Trim()))
        {
            ShowBox.MessageBox(this.Page, strMessage);
        }
    }

    /// <summary>
    /// 第一次加载页面的设置
    /// </summary>
    private void SetFirstPageLoad()
    {

        try
        {
            if (Request.QueryString["Mode"] == "Add")
            {

                panelContinue.Visible = true;
                lblTitle.Text = "物料类别管理-类别新增";

                //获取新的父类别的ID编码
                this.txtTypeID.Text = Nova_BLL_MaterialType.GetNewParentTypeID(out strMessage);
                //没有获取到ID编码
                if (string.IsNullOrEmpty(this.txtTypeID.Text.Trim()))
                {
                    ShowBox.MessageBox(this.Page, strMessage);
                }
            }
            else if (Request.QueryString["Mode"] == "Update")
            {
                lblTitle.Text = "物料类别管理-类别修改";
                panelContinue.Visible = false;
                ddlParentType.Enabled = false;   // 物料类别所属的父类别 不允许选择更改  
                txtTypeID.ReadOnly = true;
                ds = Nova_BLL_MaterialType.ListMaterialType(Request.QueryString["TypeID"]);//前一个页面传过来的参数TypeID 物料类别ID

                if (ds.Tables[0].Rows.Count > 0)
                {
                    txtTypeID.Text = Request.QueryString["TypeID"];
                    txtTypeName.Text = ds.Tables[0].Rows[0]["TypeName"].ToString().Trim();
                    ddlParentType.SelectedValue = ds.Tables[0].Rows[0]["ParentTypeID"].ToString().Trim();

                }
            }
        }
        catch
        {
            ShowBox.MessageBox(this.Page, "第一次页面加载异常跳出");

        }



    }

    protected void ibtnContinue_Click(object sender, ImageClickEventArgs e)
    {
        int res = 0;
        strMessage = "";
        try
        {
            res = Nova_BLL_MaterialType.InsertMaterialType(txtTypeID.Text.Trim(), txtTypeName.Text.Trim(), ddlParentType.SelectedValue, out strMessage);
            if (res == 1) //添加成功
            {
                txtTypeID.Text = "";
                txtTypeName.Text = "";
                ddlParentType.SelectedIndex = 0;

            }
            else    //添加失败
            {
                ShowBox.MessageBox(this.Page, strMessage);
            }


        }
        catch (Exception ex)
        {
            ShowBox.MessageBox(this.Page, "继续增加异常跳出");
        }
    }

    protected void ibtnClose_Click(object sender, ImageClickEventArgs e)
    {
        this.Page.ClientScript.RegisterStartupScript(typeof(string), "Nova", "top.close();", true);
    }
}